const functions = {
    prev() {
        return this.go(this.index - 1);
    },
    next() {
        return this.go(this.index + 1);
    },
    clear() {
        this.index = -1;
        this.totalPages = new Map();
    },
    //async request(index) {}

    // go: 跳转，遇到没有更新的进行更新
    async go(index) {
        if (this.lock) {
            console.log("触及保护锁");
            return false;
        } else {
            this.lock = true;
            let page;
            if (this.totalPages.has(index)) {
                page = this.totalPages.get(index);
            } else {
                page = await this.request(index);
                this.totalPages.set(index, page);
            }
            this.splice(0, this.length, ...page);
            this.index = index;
            this.lock = false;
            return true;
        }
    },
    // refresh: 只刷新，不进行跳转
    async refresh(index) {
        const result = await this.request(index);
        this.totalPages.set(index, result);
        // 如果是刷新同一页面，那么需要进行跳转操作刷新
        if (this.index === index) this.go(index);
    },
};
export default class Query extends Array {
    lock = false; // 保护锁，防止同时存在多个 go 操作导致混乱

    index = -1; // -1 表示未开始; 以 0 为第一位
    totalPages = new Map();
    constructor(options) {
        super();
        Object.assign(this, options, functions);
    }
}
